import { IBaseContentPage } from '@lib/IBaseIssuePage';
import { SSLib } from '@lib/SSLib';
import * as $ from 'jquery';
import './issuesNew.less';

export class IssuesNew implements IBaseContentPage {
    constructor() {
        ;
    }

    public IsMatch(url: string) {
        return /\/issues\/new($|\?)/.test(url);
    }

    public Execute() {
        initSelectDom();
        initSelectDefault();
    }
}

let projects: ProjectModel[] = [];
let tasks: TaskTypeModel[] = [];

let $issueTitle = $("#issue_title");
let $target = $('.page-title');
let $selectProjects = $("<select/>", { class: "slayer-select-project" });
let $selectTasks = $("<select/>", { class: "slayer-select-task" });

function initSelectDom() {
    projects.forEach(item => {
        $selectProjects.append(`<option value="${item.ShortName}" title="${item.Description}">${item.ShortName}</option`);
    });

    tasks.forEach(item => {
        $selectTasks.append(`<option value="${item.TaskType}" title="${item.Description}">${item.TaskType}</option`);
    });

    $target.after($selectTasks);
    $target.after($selectProjects);
    
    $selectTasks.change(autoFixTitle);
    $selectProjects.change(autoFixTitle);

    let $btn = $('.qa-issuable-create-button');
    $btn.click(item => {
        let isMatch = /^.+?\-.+?：/.test($issueTitle.val() as string);
        if (!isMatch) {
            alert("标题命名不正确\r\n格式{项目短名称或缩写}-{工作类型}：{模块与功能摘要}\r\n示例：LWP-接口对接：****功能开发")
            return false;
        }
    });
}

function initSelectDefault() {
    let matchItem: ProjectModel;

    for (let item of projects) {
        if (!item.MatchRepositorys || item.MatchRepositorys.length <= 0) {
            continue;
        }
        
        if (item.MatchRepositorys.find(a => location.href.indexOf(`/${a}/issues/new`) > 0)) {
            matchItem = item;
            break;
        }
    }

    if (!matchItem) {
        return;
    }

    // $selectProjects.find(`option[value='${matchItem.ShortName}']`).prop("selected", true);
    $selectProjects.val(matchItem.ShortName).change();
}

function autoFixTitle() {
    let project = $selectProjects.val();
    let task = $selectTasks.val();

    let originTitle = $issueTitle.val() as string;

    let pureTitle = originTitle.replace(/^.+?\-.+?：/, '');

    $issueTitle.val(`${project}-${task}：${pureTitle}`);
}

projects.push({
    ShortName: "PMS",
    Description: "跟PMS系统相关的一水项目都可以使用这个前缀，子项目则属于模块组成，有认为不妥的再讨论",
});

projects.push({
    ShortName: "PMS助手",
    Description: "PMS助手手机端、APP WEB API与APP SOCKET服务都归为一类"
});

projects.push({
    ShortName: "LWP",
    Description: "熊小智企业服务平台产品线下项目",
    MatchRepositorys: ["lwp-mp-apis"]
});

projects.push({
    ShortName: "PSC",
    Description: "PSC熊猫智云产品线下WEB项目"
});
projects.push({
    ShortName: "PTS",
    Description: "PTS 标签服务"
});

projects.push({
    ShortName: "PSC助手",
    Description: "熊猫智云预览版安卓APP、APP WEB API与APP SOCKET服务都归为一类"
});

projects.push({
    ShortName: "语音转写",
    Description: "通话录音转写文本服务"
});

projects.push({
    ShortName: "文件服务",
    Description: "文件上传服务"
});

projects.push({
    ShortName: "微信语音",
    Description: "微信语音转写文本服务"
});

// =============================
tasks.push({
    TaskType: "前端页面",
    Description: "纯前端修改或新增开发的路由（URL）级任务；"
});

tasks.push({
    TaskType: "前端组件",
    Description: "纯前端修改或新增开发的非路由（URL）级控件任务；"
});

tasks.push({
    TaskType: "接口对接",
    Description: "前端界面开发完成后接入服务端接口与联调的任务；"
});

tasks.push({
    TaskType: "QS列表",
    Description: "前端配置开发的QueryShooter列表；"
});
tasks.push({
    TaskType: "QS接口",
    Description: "与QueryShooter对应的后端接口配置；"
});
tasks.push({
    TaskType: "安卓开发",
    Description: "安卓端功能修改或新增开发；"
});
tasks.push({
    TaskType: "接口设计",
    Description: "设计功能需要哪些接口、接口参数组成、接口返回数据结构；"
});
tasks.push({
    TaskType: "数据库设计",
    Description: "设计数据存储表结构；"
});
tasks.push({
    TaskType: "后端页面",
    Description: "后端修改或新增开发服务端视图的路由（URL）级功能；"
});
tasks.push({
    TaskType: "接口开发",
    Description: "服务端项目的接口开发任务；"
});
tasks.push({
    TaskType: "后端组件",
    Description: "服务端封装各项目通用功能类或类库的修改或新增开发项；"
});
tasks.push({
    TaskType: "前端BUG",
    Description: "明确的样式错乱或JS报错，即登记人能明确鉴别的前端BUG；"
});
tasks.push({
    TaskType: "后端BUG",
    Description: "登记人可明确鉴别的后端BUG可使用该类型；"
});
tasks.push({
    TaskType: "安卓BUG",
    Description: "登记人可明确鉴别的安卓端BUG可使用该类型；"
});
tasks.push({
    TaskType: "BUG",
    Description: "登记人不能却分错误是前端还是后端时可以此类登记，先由BUG所在功能负责人排查，对BUG定性后，发送任务完结公式，再修改议题标题，由林波重新向BUG责任端发布执行；"
});
tasks.push({
    TaskType: "代码审查",
    Description: "审查代码或排查线上代码是否存在疑似的BUG；"
});
tasks.push({
    TaskType: "服务器配置",
    Description: "修改或新增服务器上的服务配置文件；"
});

class ProjectModel {
    public ShortName: string;

    public Description: string;

    public MatchRepositorys?: string[];
}

class TaskTypeModel {
    public TaskType: string;

    public Description: string;
}
